package com.itown.expo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.itown.expo.controller.base.MyBaseCommandController;
import com.itown.expo.model.Decl;
import com.itown.expo.model.DeclPrep;
import com.itown.expo.model.GoodsPrep;
import com.itown.expo.model.UnvalidDecl;

//预览提取报检单
public class HandGetDeclController extends MyBaseCommandController {

	@Override
	protected ModelAndView handleInner(HttpServletRequest request,
			HttpServletResponse response, Object command)
			throws RuntimeException {
		Decl cdecl = (Decl) command;
		Map map = new HashMap();
		map.put("cdecl", cdecl);

		Decl decl = getServiceContainer().getCheckBizService()
				.findDeclByDeclNo(cdecl.getDeclNo());
		if (decl != null) {
			cdecl.setErrorInfo("该报检单已存在于本系统中");
			return new ModelAndView("FetchDecl.jsp", "map", map);
		}

		UnvalidDecl unvalidDecl = getServiceContainer().getCheckBizService()
				.findSingleUnvalidDecl(cdecl.getDeclNo());
		if (unvalidDecl != null) {
			cdecl.setErrorInfo("该报检单已被人为设定为无效单，不能从CIQ中提取");
			return new ModelAndView("FetchDecl.jsp", "map", map);
		}

		getServiceContainer().getCheckBizService().updateFetchDeclPrep(
				cdecl.getDeclNo());

		DeclPrep declPrep = getServiceContainer().getCheckBizService()
				.findDeclPrepByDeclNo(cdecl.getDeclNo());
		if (declPrep == null) {
			cdecl.setErrorInfo("从CIQ中不能检索到此报检号");
			return new ModelAndView("FetchDecl.jsp", "map", map);
		}

		List<GoodsPrep> goodsList = getServiceContainer().getCheckBizService()
				.findGoodsPrepByDeclNo(cdecl.getDeclNo());
		map.put("declPrep", declPrep);
		map.put("goodsList", goodsList);
		return new ModelAndView("FetchDecl.jsp", "map", map);
	}
}
